package com.sskj.mine.account;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;
import com.sskj.mine.R;
import com.sskj.mine.bean.AccountChoose;
import com.sskj.mine.bean.MineAccount;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.presenter.BasePresenter;
import com.sskj.mine.account.AccountManagerActivity;

import java.util.List;

import io.reactivex.Flowable;


/**
 * @author Hey
 * Create at  2019/05/03
 */
class AccountManagerPresenter extends BasePresenter<AccountManagerActivity> {
    // 查询
    public Flowable<List<MineAccount.Account>> getAccount(int p, int size) {
        return OkGo.<HttpData<MineAccount>>get(HttpConfig.BASE_URL + HttpConfig.ACCOUNT_ALL)
                .params("p", p)
                .params("size", size)
                .converter(new JsonConverter<HttpData<MineAccount>>() {
                })
                .adapt(new FlowableBody<>())
                .map(haiPageBeanHttpData -> haiPageBeanHttpData.getData().getLists());
    }

    public void chooseAccount(String id) {
        OkGo.<HttpData<AccountChoose>>get(HttpConfig.BASE_URL + HttpConfig.ACCOUNT_CHOOSE)
                .params("auth_id", id)
                .execute(new JsonCallBack<HttpData<AccountChoose>>(this, false) {
                    @Override
                    public void onSuccess(Response<HttpData<AccountChoose>> response) {
                        HttpData<AccountChoose> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateToken(httpData.getData());
                            ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_accountManagerPresenter1_j));
                        }
                    }
                });
    }
}
